package cc.linker.steplink.crm.merchanth5.service;

import cc.linker.steplink.crm.merchanth5.dao.SupplierDao;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author huhj
 */
@Service
@Slf4j
public class SupplierService {


    @Autowired
    private SupplierDao supplierDao;

    public Map<String, Object> getInfo(Map<String, Object> param) {
        Map<String, Object> goodsInfoShipping = supplierDao.getGoodsInfoByGoodsId(param);
        if(null == goodsInfoShipping){
            throw GlobalException.build("参数错误");
        }
        if(!StringUtils.isEmpty(goodsInfoShipping.get("mallsGoodsId"))){
            param.put("goodsId",goodsInfoShipping.get("mallsGoodsId"));
        }else{
            throw GlobalException.build("参数错误");
        }
        Map<String, Object> goodsInfoMall = supplierDao.getGoodsInfoByGoodsId(param);
        if(StringUtils.isEmpty(goodsInfoMall.get("belongId"))){
            param.put("customerId",goodsInfoMall.get("orgId"));
        }else{
            param.put("customerId",goodsInfoMall.get("belongId"));
        }
        param.put("orgId",goodsInfoMall.get("orgId"));
        Map<String, Object> info = supplierDao.getInfo(param);
        if(null == info){
            throw GlobalException.build("未找到供货商信息");
        }
        if(param.get("orgId").equals(param.get("customerId"))){
            param.remove("customerId");
        }
        LOGGER.info("getMerchantInfo-------param:"+ JSON.toJSONString(param));
        Map<String, Object> merchantInfo = supplierDao.getMerchantInfo(param);
        if(null != merchantInfo){
            info.put("socialCreditImage",merchantInfo.get("socialCreditImage"));
        }else{
            info.put("socialCreditImage","");
        }
        return info;
    }
}
